package cn.schoolwow.flow.monitor.module.business.testcase.flow;

import cn.schoolwow.ams.domain.option.QuickAMSOption;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.domain.TestCaseFlow;
import cn.schoolwow.flow.monitor.entity.TestCase;
import cn.schoolwow.quickdao.domain.database.dml.CompareInstanceListResult;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.queryPackage.QueryPackage;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ResetTestCaseFlow implements BusinessFlow {

    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);
        QuickAMSOption quickAMSOption = applicationContext.getBean(QuickAMSOption.class);

        List<TestCaseFlow> testCaseFlowList = QueryPackage.newQuery(flowMonitorOption.basePackageName)
                .addClassQuery(TestCaseFlow.class)
                .execute()
                .getInstanceList(TestCaseFlow.class);
        if(null==testCaseFlowList||testCaseFlowList.isEmpty()){
            flowMonitorOption.dao.clear(TestCase.class);
            return;
        }

        List<TestCase> testCaseList = new ArrayList<>();
        for(TestCaseFlow testCaseFlow:testCaseFlowList){
            TestCase testCase = new TestCase();
            testCase.setCategoryName("其他");
            for(Map.Entry<String,String> entry:quickAMSOption.packageNameLabelMap.entrySet()){
                String className = testCaseFlow.getClass().getName();
                if(className.contains(entry.getKey())){
                    testCase.setCategoryName(entry.getValue());
                }
            }
            BusinessFlow businessFlow = testCaseFlow;
            testCase.setName(businessFlow.name());

            TestCase testCase1 = flowMonitorOption.dao.query(TestCase.class)
                    .addQuery("categoryName", testCase.getCategoryName())
                    .addQuery("name", testCase.getName())
                    .execute()
                    .getOne();
            if(null!=testCase1){
                testCaseList.add(testCase1);
            }else{
                testCaseList.add(testCase);
            }
        }

        List<TestCase> testCaseTotalList = flowMonitorOption.dao.query(TestCase.class).execute().getList();
        CompareInstanceListResult compareInstanceListResult = flowMonitorOption.dao.compareInstanceList(testCaseList, testCaseTotalList);
        flowMonitorOption.dao.insert(compareInstanceListResult.insertInstanceList);
        flowMonitorOption.dao.update(compareInstanceListResult.updateInstanceList);
        flowMonitorOption.dao.delete(compareInstanceListResult.deleteInstanceList);
    }

    @Override
    public String name() {
        return "重置测试用例表";
    }
}
